package guis;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connectionPool.PoolException;

import sql.SQLs;

/**
 * Servlet implementation class KursRegistrieren
 */
@WebServlet("/FeedbackErfassen")
public class FeedbackErfassen extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FeedbackErfassen() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Feedback f = (Feedback) request.getSession().getAttribute("feedback");
			String kursdatum = f.getModul();
			System.out.println(kursdatum);
			String[] kursdatumarr = kursdatum.split(",");
			ResultSet df = SQLs.auswaehlen("durchfuehrungID", "durchfuehrung", "von = \"" + kursdatumarr[1] + "\" AND bis =\"" + kursdatumarr[2] + "\"");
			if (df.next()){
				int teilid = SQLs.insertIntoTeilnahme(f.getName(), f.getVorname(), df.getString("durchfuehrungID"));
				int fbID = SQLs.insertIntoFeedback(f.getWaswargut(), f.getWaswarwenigergut(), f.getWuensche(), f.getBemerkung(), teilid);
				SQLs.insertIntoKlickFeedback(3, f.getBez3bew(), f.getBez3bem(), fbID);
				SQLs.insertIntoKlickFeedback(2, f.getBez2bew(), f.getBez2bem(), fbID);
				SQLs.insertIntoKlickFeedback(4, f.getBez4bew(), f.getBez4bem(), fbID);
				SQLs.insertIntoKlickFeedback(5, f.getBez5bew(), f.getBez5bem(), fbID);
				SQLs.insertIntoKlickFeedback(6, f.getBez6bew(), f.getBez6bem(), fbID);
			}
			
			response.sendRedirect("/SVNFeedbackTool/Main.jsp");
		} catch (PoolException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	


}
